//
//  CYScanCodeVC.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/9/23.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//
#define offsetX AL(PT(40))
#import "CYScanCodeVC.h"
#import "CYRechargeTipLBL.h"
@interface CYScanCodeVC()<CYTextFieldDelegate>{
    scanCodeBlock _myblock;//
}
@property (nonatomic, strong) CYRechargeTipLBL *tipsLBL;//最顶处的提示
@property (nonatomic, strong) CYImageView *QRCodeIMG;//二维码
@property (nonatomic, strong) CYLabel *nameLBL;//姓名
@property (nonatomic, strong) CYLabel *bankLBL;//银行
@property (nonatomic, strong) CYLabel *accountLBL;//卡号
@property (nonatomic, strong) CYTextField *nameField;//支付户名
@property (nonatomic, strong) CYTextField *accountField;//支付账号
@property (nonatomic, strong) CYTextField *moneyField;//支付金额
@property (nonatomic, strong) CYLabel *payNameLBL;//支付户名
@property (nonatomic, strong) CYLabel *payAccountLBL;//支付账号
@property (nonatomic, strong) CYLabel *payMoneyLBL;//支付金额
@property (nonatomic, strong) CYLabel *payTimeLBL;//支付时间
@property (nonatomic, strong) CYButton *submitBtn;//提交按钮
@property (nonatomic, strong) CYButton *saveBtn;//保存二维码
@property (nonatomic, strong) CYButton *jumpBtn;//跳转到相应的app
@property (nonatomic, strong) CYLabel *remindLBL;//底部的提醒
@property (nonatomic, strong) CYView *alphaView;//输入状态，点击的透明view ,放弃第一响应身份
@property (nonatomic, copy) NSString *title;

@end
@implementation CYScanCodeVC
- (instancetype)initWithTitle:(NSString *)title block:(scanCodeBlock)block{
    self = [self initWithColor:[UIColor whiteColor]];
    if (self) {
        _myblock = block;
        _title = title;
        self.tipsLBL.hidden = NO;
        self.QRCodeIMG.hidden = NO;
        self.saveBtn.hidden = self.jumpBtn.hidden = NO;
        self.nameLBL.hidden = NO;
        self.bankLBL.hidden = NO;
        self.accountLBL.hidden = NO;
        self.nameField.hidden = self.accountField.hidden = self.moneyField.hidden = self.timeField.hidden = NO;
        self.submitBtn.hidden = NO;
        self.remindLBL.hidden = NO;
        self.alphaView.hidden = YES;
    }
    return self;
}

- (CYRechargeTipLBL *)tipsLBL{
    if (!_tipsLBL) {
        _tipsLBL = [[CYRechargeTipLBL alloc]initWithText:@"尊敬的彩云会员,在线支付二维码如遇到无法正常支付,为了避免耽误广大用户下注,请选择支付宝都是立即到账，如有疑问,请及时联系在线客服" size:AL(PT(22)) textcolor:TEXT_GRAY_COLOR];
        _tipsLBL.layer.masksToBounds = YES;
        _tipsLBL.layer.cornerRadius = 5.0;
        _tipsLBL.layer.borderColor = TEXT_GRAY_COLOR.CGColor;
        _tipsLBL.layer.borderWidth = 0.5;
        _tipsLBL.backgroundColor = LINE_COLOR;
        _tipsLBL.numberOfLines = 0;
        [_tipsLBL sizeToFit];
        [self addSubview:_tipsLBL];
        
        [_tipsLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(AL(PT(20)));
            make.left.equalTo(self).with.offset(offsetX);
            make.right.equalTo(self).with.offset(- offsetX);
            make.height.mas_equalTo([_tipsLBL autoSizeWithWidth:SCREEN_WIDTH - offsetX * 2].height + AL(20));
        }];
    }
    return _tipsLBL;
}

- (CYImageView *)QRCodeIMG{
    if (!_QRCodeIMG) {
        _QRCodeIMG = [[CYImageView alloc]init];
        _QRCodeIMG.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH / 2.0, SCREEN_WIDTH / 2.0);
        _QRCodeIMG.loadingView.hidden = NO;
        _QRCodeIMG.layer.backgroundColor = LINE_COLOR.CGColor;
        [self addSubview:_QRCodeIMG];
        
        [_QRCodeIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_tipsLBL.mas_bottom).with.offset(AL(PT(10)));
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 2.0, SCREEN_WIDTH / 2.0));
        }];
    }
    return _QRCodeIMG;
}

- (CYButton *)saveBtn{
    if (!_saveBtn) {
        _saveBtn = [[CYButton alloc]initWithTitle:@"保存二维码" titleColor:[UIColor whiteColor] backgroundColor:THEME_COLOR cornerRadius:5.0];
        [_saveBtn addTarget:self action:@selector(actionForSaveBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_saveBtn];
        
        [_saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_QRCodeIMG.mas_bottom).with.offset(AL(PT(20)));
            make.left.equalTo(self).with.offset(offsetX);
            make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - offsetX * 2.5) / 2.0, AL(44)));
        }];
    }
    return _saveBtn;
}

- (CYButton *)jumpBtn{
    if (!_jumpBtn) {
        _jumpBtn = [[CYButton alloc]initWithTitle:[NSString stringWithFormat:@"跳转%@",_title] titleColor:[UIColor whiteColor] backgroundColor:THEME_COLOR cornerRadius:5.0];
        [_jumpBtn addTarget:self action:@selector(actionForJumpBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_jumpBtn];
        
        [_jumpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_QRCodeIMG.mas_bottom).with.offset(AL(PT(20)));
            make.right.equalTo(self).with.offset(- offsetX);
            make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - offsetX * 2.5) / 2.0, AL(44)));
        }];
    }
    return _jumpBtn;
}
- (CYLabel *)nameLBL{
    if (!_nameLBL) {
        _nameLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"姓名:%@",_dict[@"bankAccount"]] size:AL(PT(22)) textcolor:TEXT_BLACK_COLOR];
        [self addSubview:_nameLBL];
        
        [_nameLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_saveBtn.mas_bottom).with.offset(AL(PT(10)));
            make.right.equalTo(self).with.offset(- AL(PT(20)) - SCREEN_WIDTH / 2.0);
            make.size.mas_equalTo(_nameLBL.bounds.size);
        }];
    }
    return _nameLBL;
}

- (CYLabel *)bankLBL{
    if (!_bankLBL) {
        _bankLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"开户银行:%@",_dict[@"bankName"]] size:AL(PT(22)) textcolor:TEXT_BLACK_COLOR];
        [self addSubview:_bankLBL];
        
        [_bankLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_saveBtn.mas_bottom).with.offset(AL(PT(10)));
            make.left.equalTo(self).with.offset(AL(PT(20)) + SCREEN_WIDTH / 2.0);
            make.size.mas_equalTo(_bankLBL.bounds.size);
        }];
    }
    return _bankLBL;
}

- (CYLabel *)accountLBL{
    if (!_accountLBL) {
        NSString *string = [NSString spaceBetweenFourCharacters:_dict[@"bankNo"]];
        _accountLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"卡号:%@",string] size:AL(PT(22)) textcolor:TEXT_BLACK_COLOR];
        [self addSubview:_accountLBL];
        
        [_accountLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_nameLBL.mas_bottom).with.offset(0);
            make.left.equalTo(_nameLBL);
            make.size.mas_equalTo(_accountLBL.bounds.size);
        }];
    }
    return _accountLBL;
}

- (CYTextField *)nameField{
    if (!_nameField) {
        _nameField = [[CYTextField alloc] initWithPlaceholder:@"请输入支付宝用户名   如:王xx" color:TEXT_GRAY_COLOR size:AL(PT(22))];
        _nameField.borderStyle = UITextBorderStyleRoundedRect;
        _nameField.cydelegate = self;
        _nameField.tag = 200;
        [self addSubview:_nameField];
        
        [_nameField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_accountLBL.mas_bottom).with.offset(offsetX);
            make.right.equalTo(self).with.offset(- offsetX);
            make.left.equalTo(self.payNameLBL.mas_right).with.offset(AL(PT(10)));
            make.height.mas_equalTo(AL(44));
        }];
    }
    return _nameField;
}

- (CYTextField *)accountField{
    if (!_accountField) {
        _accountField = [[CYTextField alloc] initWithPlaceholder:@"请输入支付账号" color:TEXT_GRAY_COLOR size:AL(PT(22))];
        _accountField.borderStyle = UITextBorderStyleRoundedRect;
        _accountField.cydelegate = self;
        _accountField.tag = 201;
        [self addSubview:_accountField];
        
        [_accountField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_nameField.mas_bottom).with.offset(AL(PT(20)));
            make.right.equalTo(self).with.offset(- offsetX);
            make.left.equalTo(self.payAccountLBL.mas_right).with.offset(AL(PT(10)));
            make.height.mas_equalTo(AL(44));
        }];
    }
    return _accountField;
}

- (CYTextField *)moneyField{
    if (!_moneyField) {
        _moneyField = [[CYTextField alloc] initWithPlaceholder:@"请输入支付金额  如:888.88" color:TEXT_GRAY_COLOR size:AL(PT(22))];
        _moneyField.borderStyle = UITextBorderStyleRoundedRect;
        _moneyField.cydelegate = self;
        _moneyField.tag = 202;
        [self addSubview:_moneyField];
        
        [_moneyField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_accountField.mas_bottom).with.offset(AL(PT(20)));
            make.right.equalTo(self).with.offset(- offsetX);
            make.left.equalTo(self.payMoneyLBL.mas_right).with.offset(AL(PT(10)));
            make.height.mas_equalTo(AL(44));
        }];
    }
    return _moneyField;
}

- (CYTextField *)timeField{
    if (!_timeField) {
        _timeField = [[CYTextField alloc] initWithPlaceholder:@"请输入存款时间  如:2017-09-13" color:TEXT_GRAY_COLOR size:AL(PT(22))];
        _timeField.borderStyle = UITextBorderStyleRoundedRect;
        _timeField.cydelegate = self;
        _timeField.tag = 203;
        [self addSubview:_timeField];
        
        [_timeField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_moneyField.mas_bottom).with.offset(AL(PT(20)));
            make.right.equalTo(self).with.offset(- offsetX);
            make.left.equalTo(self.payTimeLBL.mas_right).with.offset(AL(PT(10)));
            make.height.mas_equalTo(AL(44));
        }];
    }
    return _timeField;
}

- (CYLabel *)payNameLBL{
    if (!_payNameLBL) {
        _payNameLBL = [[CYLabel alloc]initWithText:@"支付户名" size:AL(PT(22)) textcolor:TEXT_BLACK_COLOR];
        [self addSubview:_payNameLBL];
        
        [_payNameLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_nameField);
            make.left.equalTo(self).with.offset(offsetX);
            make.size.mas_equalTo(_payNameLBL.bounds.size);
        }];
    }
    return _payNameLBL;
}

- (CYLabel *)payAccountLBL{
    if (!_payAccountLBL) {
        _payAccountLBL = [[CYLabel alloc]initWithText:@"支付账号" size:AL(PT(22)) textcolor:TEXT_BLACK_COLOR];
        [self addSubview:_payAccountLBL];
        
        [_payAccountLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_accountField);
            make.left.equalTo(self).with.offset(offsetX);
            make.size.mas_equalTo(_payAccountLBL.bounds.size);
        }];
    }
    return _payAccountLBL;
}

- (CYLabel *)payMoneyLBL{
    if (!_payMoneyLBL) {
        _payMoneyLBL = [[CYLabel alloc]initWithText:@"支付金额" size:AL(PT(22)) textcolor:TEXT_BLACK_COLOR];
        [self addSubview:_payMoneyLBL];
        
        [_payMoneyLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_moneyField);
            make.left.equalTo(self).with.offset(offsetX);
            make.size.mas_equalTo(_payMoneyLBL.bounds.size);
        }];
    }
    return _payMoneyLBL;
}

- (CYLabel *)payTimeLBL{
    if (!_payTimeLBL) {
        _payTimeLBL = [[CYLabel alloc]initWithText:@"支付时间" size:AL(PT(22)) textcolor:TEXT_BLACK_COLOR];
        [self addSubview:_payTimeLBL];
        
        [_payTimeLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_timeField);
            make.left.equalTo(self).with.offset(offsetX);
            make.size.mas_equalTo(_payTimeLBL.bounds.size);
        }];
    }
    return _payTimeLBL;
}

- (CYButton *)submitBtn{
    if (!_submitBtn) {
        _submitBtn = [[CYButton alloc]initWithTitle:@"提交" titleColor:[UIColor whiteColor] backgroundColor:THEME_COLOR cornerRadius:5.0];
        [_submitBtn addTarget:self action:@selector(actionForSubmitBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_submitBtn];
        
        [_submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_timeField.mas_bottom).with.offset(AL(PT(20)));
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - offsetX * 2, AL(44)));
        }];
    }
    return _submitBtn;
}

- (CYLabel *)remindLBL{
    if (!_remindLBL) {
        _remindLBL = [[CYLabel alloc]initWithText:@"温馨提示:\n1.存款步骤:先查看要入款的银行账号信息，然后通过网上银行,ATM,柜台或手机银行进行转账，转账成功后再如实提交转账信息，财务专员查收到信息后会及时添加您的款项。\n2.请转账时务必增加小数(如:500.77元),以便财务查款。\n3.请尽可能选择同行办理转账,可快速到账。\n4.请保留好转账单据作为核对证明" size:AL(PT(22)) textcolor:TEXT_GRAY_COLOR];
        _remindLBL.numberOfLines = 0;
        [self addSubview:_remindLBL];
        
        [_remindLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_submitBtn.mas_bottom).with.offset(offsetX);
            make.bottom.equalTo(self).with.offset(- offsetX);
            make.centerX.equalTo(self);
            make.size.mas_equalTo([_remindLBL autoSizeWithWidth:SCREEN_WIDTH - offsetX * 2]);
        }];
    }
    return _remindLBL;
}

- (CYView *)alphaView{
    if (!_alphaView) {
       _alphaView = [[CYView alloc]initWithColor:[UIColor clearColor]];
       UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        [_alphaView addGestureRecognizer:tap];
        [self addSubview:_alphaView];
        
        [_alphaView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0));
        }];
    }
    return _alphaView;
}

- (void)setModel:(CYRechergeModel *)model{
    if (_model != model) {
        _model = model;
        [CYHttps loadingWithFile:_model.imgAddress complete:^(UIImage* data) {
            _QRCodeIMG.image = [UIImage imageWithImage:data scaledToSize:_QRCodeIMG.bounds.size];//压缩图片
            CGFloat width = data.size.width * 1.0 / data.size.height * SCREEN_WIDTH / 2.0;
            [_QRCodeIMG mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(width, SCREEN_WIDTH / 2.0));
            }];
        }];
        _nameLBL.text = [NSString stringWithFormat:@"姓名:%@" ,_model.accountName];
        _bankLBL.text = [NSString stringWithFormat:@"开户银行:%@", _model.bankName];
        _accountLBL.text = [NSString stringWithFormat:@"卡号:%@", _model.bankNo];
        [self updateNameLBLBounds];
        [self updateBankLBLBounds];
        [self updateAccountLBLBounds];
    }
}

- (void)updateNameLBLBounds{
    
    [_nameLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_nameLBL.bounds.size);
    }];
}

- (void)updateBankLBLBounds{
    
    [_bankLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_bankLBL.bounds.size);
    }];
}

- (void)updateAccountLBLBounds{
    
    [_accountLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_accountLBL.bounds.size);
    }];
}

//应用内app跳转
- (void)actionForJumpBtn{
    _myblock(jumpForScanCodeEvent,nil);
}

//提交
- (void)actionForSubmitBtn{
    _myblock(subMitForScanCodeEvent,nil);
}

//保存到相册
- (void)actionForSaveBtn{
    _myblock(saveForScanCodeEvent,_QRCodeIMG.image);
}

- (void)tapAction{
    [self endEditing:YES];
    _alphaView.hidden = YES;
}

#pragma - mark - CYUITextFieldDelegate - method
- (void)heightWhenKeyboardChange:(CGFloat)height animationInterval:(float)time{
    if (height > 0)
        [UIView animateWithDuration:time animations:^{
            self.transform = CGAffineTransformMakeTranslation(0.0, -height);
        }];
    else
        [UIView animateWithDuration:time animations:^{
            self.transform  = CGAffineTransformIdentity;
        }];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    textField.layer.cornerRadius = 5.0;
    textField.layer.borderWidth = 0.8;
    textField.layer.borderColor = THEME_COLOR.CGColor;
    textField.layer.masksToBounds = YES;
    _alphaView.hidden = NO;
    if ([textField.placeholder containsString:@"时间"]){
        _myblock(showDateListForScanCodeEvent,nil);
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(CYTextField *)textField{
    if (textField.tag < 203) {
        CYTextField *nextTextField  = [self viewWithTag:textField.tag + 1];
        [nextTextField becomeFirstResponder];
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    textField.layer.borderWidth = 0.0;
    textField.layer.borderColor = [UIColor clearColor].CGColor;
    _alphaView.hidden = YES;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
